package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * Author Xu Chen xu
 * Create 2024/4/20 - 下午2:07
 * version v1.0
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/acl/user")
@CrossOrigin //跨域
public class AdminController {
    @Resource
    private AdminService adminService;

    @Resource
    private RoleService roleService;

    @ApiOperation("用户列表")
    @GetMapping("{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       AdminQueryVo adminQueryVo) {
        Page<Admin> pageParam = new Page<>(page, limit);
        IPage<Admin> pageModel = adminService.selectPageUser(pageParam, adminQueryVo);
        return Result.ok(pageModel);
    }

    //id查询用户
    @ApiOperation("根据id查询")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        return Result.ok(adminService.getById(id));
    }

    //添加用户
    @ApiOperation("添加用户")
    @PostMapping("save")
    public Result save(@RequestBody Admin admin) {
        String password = MD5.encrypt(admin.getPassword());
        admin.setPassword(password);
        return Result.ok(adminService.save(admin));
    }

    //修改用户
    @ApiOperation("修改用户")
    @PutMapping("update")
    public Result updateById(@RequestBody Admin admin) {
        return Result.ok(adminService.updateById(admin));
    }

    //删除用户
    @ApiOperation("根据id删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        return Result.ok(adminService.removeById(id));
    }

    //批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean batchRemove = adminService.removeByIds(idList);
        if (batchRemove) {
            return Result.ok(null);
        }else {
            return Result.fail(null);

        }
    }


    //获取角色数据
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId) {
        Map<String, Object> roleMap = roleService.findRoleByUserId(adminId);
        return Result.ok(roleMap);
    }

    //给用户进行角色分配
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long adminId,@RequestParam Long[] roleId) {
        roleService.saveUserRoleRelationShip(adminId,roleId);
        return Result.ok(null);
    }
}
